<template>
  <div class="com-container">
    <div class="com-chart" ref="hot_ref"></div>
    <span class="iconfont arr-left" @click="toLeft" :style="comStyle">＜</span>
    <span class="iconfont arr-right" @click="toRight" :style="comStyle">＞</span>
    <span class="cat-name" :style="comStyle">{{catname}}</span>
  </div>
</template>

<script>
export default {
  data() {
    return {
      charInstance: null,
      allData: null,
      currentIndex:0,
      titleFontSize:0
    };
  },
  computed:{
      catname(){
          if(!this.allData){
              return ''
          }else{
              return this.allData[this.currentIndex].name
          }
      },
      comStyle(){
          return {
              fontSize:this.titleFontSize+'px'
          }
      }
  },
    created () {
    // 在组件创建完成之后 进行回调函数的注册
    this.$socket.registerCallBack('hotData', this.getData)
  },
  mounted() {
    this.iniChart();
       this.$socket.send({
      action: 'getData',
      socketType: 'hotData',
      chartName: 'hot',
      value: ''
    })
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    this.$socket.unRegisterCallBack('hotData')
  },

  methods: {
    iniChart() {
      this.charInstance = this.$echarts.init(this.$refs.hot_ref,'chalk');
      const initOption = {
        title:{
            text:'▎热销商品的占比',
            left:20,
            top:20,
        },
        legend:{
            top:'15%',
            icon:'circle'
        },
        tooltip:{
            show:true,
            formatter:arg=>{
               const thirdCategory=arg.data.children;
               let total=0;
               thirdCategory.forEach(item=>{
                   total+=item.value
               })
               let retStr='';
               thirdCategory.forEach(item=>{
                   retStr+=`
                   ${item.name}:${parseInt(item.value/total*100)+'%'}
                   <br>
                   `
               })
                return retStr
            }
        },
        series: [
          {
            type: "pie",
            label:{ 
                show:false
            },
            // 高亮显示
            emphasis:{
                label:{
                    show:true
                },
                labelLine:{
                    show:false
                }
            }
          },
        ],
      };
      this.charInstance.setOption(initOption);
    },
    async getData(ret) {
      //const { data: ret } = await this.$http.get("hot");
      this.allData = ret;
    //   console.log(this.allData);
      this.updateChart();
    },
    updateChart() {
      const legenData=this.allData[this.currentIndex].children.map(item=>{
          return item.name
      })
      const seriesData = this.allData[this.currentIndex].children.map((item) => {
        return {
          name: item.name,
          value: item.value,
          children:item.children
        };
      });
      const dataOption = {
        legend:{
            data:legenData
        },
        series: [
          {
              data:seriesData
          },
        ],
      };
      this.charInstance.setOption(dataOption);
    },
    screenAdapter() {
       this.titleFontSize=this.$refs.hot_ref.offsetWidth/100*3.6;
      const adapterOption = {
          title:{
              textStyle:{
                  fontSize:this.titleFontSize
              }
          },
          legend:{
              itemWidth:this.titleFontSize,
              itemHeight:this.titleFontSize,
              itemGap:this.titleFontSize/2,
              textStyle:{
                  fontSize:this.titleFontSize
              }
          },
          series:[
              {
                 radius:this.titleFontSize*4.5,
                 center:['50%','60%'] 
              }
          ]
      };
      this.charInstance.setOption(adapterOption);
      this.charInstance.resize();
    },
    toLeft(){
       this.currentIndex--;
       if(this.currentIndex<0){
           this.currentIndex=this.allData.length-1;
       }
       this.updateChart();
    },
    toRight(){
       this.currentIndex++;
       
       if(this.currentIndex>this.allData.length-1){
           this.currentIndex=0;
       }
       this.updateChart();
    }
  },
};
</script>

<style lang='less' scoped>
.arr-left{
    color: white;
    position: absolute;
    left: 10%;
    top:50%;
    transform: translateY(-50%);
    cursor: pointer;
}
.arr-right{ 
    position: absolute;
    right: 10%;
    top:50%;
    transform: translateY(-50%);
    cursor: pointer;
    color: white;
}

.cat-name{
    position: absolute;
    left: 80%;
    bottom: 20px;
    color: white;
}
</style>